package com.lethelh.jdbc.app.command;

import com.lethelh.jdbc.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class DeleteCommand implements Command{
    @Override
    public void execute() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要删除的商品编号:");
        int id =  input.nextInt();
        Connection conn = null;
        PreparedStatement stmt = null;
        String sql = "delete from goods where id = ?";
        try {
            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            int result = stmt.executeUpdate();
            if (result == 1){
                System.out.println("删除成功!");
            }else {
                System.out.println("查无此商品!删除失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                DBUtils.closeConnection(null, stmt, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
